package com.forever7776.life.admin.controller.life.m.blog;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.blog.BlogType;
import com.forever7776.life.core.service.blog.IBlogService;
import com.forever7776.life.core.service.blog.IBlogTypeService;
import com.forever7776.life.remote.common.vo.ResultVO;
import enums.ResultEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.IdUtil;
import util.ResultUtil;

/**
 * <p>
 * 博客分类管理
 * </p>
 *
 * @author kz
 * @date 2018-02-28
 */
@Controller
@RequestMapping("/m/blogtype")
public class ManageBlogTypeController extends BaseController {

    @Autowired
    private IBlogTypeService iBlogTypeService;
    @Autowired
    private IBlogService iBlogService;

    @RequestMapping
    public String index() {
        return LIFE_ADMIN_VIEW_PAGE_PATH + "blog/blog_type";
    }

    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list() {
        JSONObject result = new JSONObject();
        result.put("page", iBlogTypeService.selectPage(new Page<>(), new EntityWrapper<BlogType>().orderBy("sort ,last_date", false)));
        return result;
    }

    @RequestMapping("/{id}")
    @ResponseBody
    public JSONObject getById(@PathVariable String id) {
        BlogType blogType = iBlogTypeService.selectById(id);
        JSONObject result = ResultUtil.getSuccessResult("");
        result.put("blogtype", blogType);
        return result;
    }

    /**
     * 添加博客类型
     *
     * @param blogType
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addRole(BlogType blogType) {
        JSONObject result = checkForm(blogType, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        blogType.setId(IdUtil.genRandomNum(6));
        iBlogTypeService.insert(blogType);
        return result;
    }

    /**
     * 编辑博客类型
     *
     * @param blogType
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateRole(@PathVariable String id, BlogType blogType) {
        if (iBlogTypeService.selectById(id) == null) {
            return ResultUtil.getFailResult("该博客类型不存在！");
        }
        JSONObject result = checkForm(blogType, "编辑成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        if (StringUtils.isEmpty(blogType.getIcon())) {
            blogType.setIcon(null);
        }
        iBlogTypeService.updateById(blogType);
        return result;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO deleteRole(@PathVariable String  id) {
        if(CollectionUtils.isNotEmpty(iBlogService.selectByBlogTypeId(id))) {
            return ResultVoUtil.getFailResult("该博客类型有~");
        }
        boolean del = iBlogTypeService.deleteById(id);
        if (del) {
            return ResultVoUtil.getSuccessResult("删除成功!");
        }
        return ResultVoUtil.getFailResult("删除失败~");
    }

    /**
     * 冻结博客类型
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/frozen/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject frozenRole(@PathVariable String id, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        iBlogTypeService.frozen(id, status);
        return ResultUtil.getSuccessResult("操作成功！");
    }

    public JSONObject checkForm(BlogType blogType, String msg) {
        if (StringUtils.isEmpty(blogType.getName())) {
            return ResultUtil.getFailResult("博客类型名称为空");
        }
        return ResultUtil.getSuccessResult(msg);
    }
}
